package com.wf.course.web.controller.goods;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.utils.page.PageBean;
import com.wf.course.model.comm.ReplyInfo;
import com.wf.course.model.goods.GoodsComment;
import com.wf.course.model.goods.GoodsCommentImg;
import com.wf.course.service.IGoodsCommentImgService;
import com.wf.course.service.IGoodsCommentService;
import com.wf.course.service.IReplyInfoService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 列表Controller
 */
@RequestMapping("goodsComment")
@Controller
public class GoodsCommentController {

	@Autowired
	private IGoodsCommentService goodsCommentService;
	@Autowired
	private IGoodsCommentImgService commentImgService;
	@Autowired
	private IReplyInfoService replyService;

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param goodsName
	 * @param model
	 * @return
	 */
	@LogInfo("查询商品评论信息")
	@RequestMapping("goodsCommentList")
	public String GoodsCommentList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "goodsName", required = false) String goodsName, 
			@RequestParam(value = "userName", required = false) String userName, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("goodsName", goodsName);
		cons.put("userName", userName);
		PageBean<GoodsComment> pageBean = new PageBean<GoodsComment>(pageNo, pageSize, null, cons);
		pageBean.setUrl("goodsComment/goodsCommentList");
		this.goodsCommentService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "goods/goodsCommentList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增/修改商品评论，页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			GoodsComment goodsComment = goodsCommentService.queryGoodsCommentById(id);
			model.addAttribute("goodsComment", goodsComment);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		 
		 List<GoodsCommentImg> goodsCommentImgs = commentImgService.queryGoodsCommentImgs(id);
		 model.addAttribute("goodsCommentImgs",goodsCommentImgs);
		 
		return "goods/goodsCommentEdit";
	}

	/**
	 * 新增和修改
	 * 
	 * @param goodsComment
	 * @param request
	 * @return
	 */
	@LogInfo("新增/修改商品评论")
	@RequestMapping(value = "GoodsCommentEdit" , produces = "text/html;charset=UTF-8")
	public String GoodsCommentEdit(@ModelAttribute GoodsComment goodsComment, HttpServletRequest request) {
		
		goodsCommentService.saveOrUpdateGoodsComment(goodsComment);
		
		return "redirect:/goodsComment/goodsCommentList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除商品评论")
	@RequestMapping(value = "remove/{id}")
	public String removeGoodsComment(@PathVariable("id") Integer id) {
		
		goodsCommentService.deleteById(id);
		
		return "redirect:/goodsComment/goodsCommentList";
	}
	
	/**
	 * 删除商品评论
	 * @param ids
	 * @return
	 */
	@LogInfo("批量删除商品评论")
	@ResponseBody
	@RequestMapping(value = { "batchdeleteComment" }, method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	public String deleteByParamIds(@RequestParam(value = "ids[]", required = false) String[] ids) {
		if(ids!=null && ids.length>0){
				try {
					if(!goodsCommentService.batchdeleteComment(ids))
						throw new Exception();
				} catch (Exception e) {
					e.printStackTrace();
					return "0";
				}
			}
		
		return "1";
		
	}
	
	
	/**
	 * 回复商品评论
	 * @param ids
	 * @return
	 */
	@LogInfo("回复商品评论")
	@ResponseBody
	@RequestMapping(value = { "replyComment" }, method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	public String replyComment(@RequestParam(value = "replyContent", required = false) String replyContent,
			@RequestParam(value = "commentId") Integer commentId) {
		 
		ReplyInfo replyInfo = new ReplyInfo();
		replyInfo.setCommentId(commentId);
		replyInfo.setReplyContent(replyContent);
		replyInfo.setReplyUserId(11);
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		replyInfo.setReplyTime(time);
		replyInfo.setReplyContent(replyContent);
		 replyInfo.setParentFlag(0);
		replyInfo.setReplyType(2);
		try {
			replyService.saveReplyInfo(replyInfo);
				throw new Exception();
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	 
		
		return null;
		
	}
	
	

}
